home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993 October: Windmill on DISC / ADC Developer CD (1993-10) (''Windmill On DISC'')_iso / Dev.CD Oct 93.iso / System Software / U.S. System Software / System 7 Pro™ Beta 11 / Development Tools / Sample Code / Standard Mail / CollaboDraw (w⁄DigiSign) / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-11  |  8.9 KB  |  452 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------------------------
  2.  *
  3.  * Simple Sample AOCE Application Framework
  4.  *
  5.  * ©1991-1993 Apple Computer
  6.  *
  7.  -------------------------------------------------------------------------------------*/
  8. /*
  9.  * commands.c -- called in response to menu commands or appleevents
  10.  *
  11.  * change history:
  12.  *
  13.  * SJF        04/21/93        1.0b2        update to b2
  14.  * SJF        03/01/93        1.0b1        added digital signatures
  15.  * SJF        02/09/93        1.0b1        update to b1
  16.  * SJF        10/13/92        1.0d4        update to a11
  17.  * SJF        09/09/92        1.0d3        update to a9
  18.  * SJF        05/07/92        1.0d2        update to a6
  19.  * SJF        11/06/91        1.0d1        initial coding
  20.  *
  21.  */
  22.  
  23. #ifndef __OCEMAIL__
  24. #include <OCEMail.h>
  25. #endif
  26.  
  27. #ifndef __OCESTANDARDMAIL__
  28. #include <OCEStandardMail.h>
  29. #endif
  30.  
  31. #ifndef __DIALOGS__
  32. #include <Dialogs.h>
  33. #endif
  34.  
  35. #ifndef __CONTROLS__
  36. #include <Controls.h>
  37. #endif
  38.  
  39. #ifndef __STANDARDFILE__
  40. #include <StandardFile.h>
  41. #endif
  42.  
  43. #ifndef __FILES__
  44. #include <Files.h>
  45. #endif
  46.  
  47. #ifndef __SCRIPT__
  48. #include <Script.h>
  49. #endif
  50.  
  51. #include "const.h"
  52. #include "strconst.h"
  53. #include "mymenus.h"
  54. #include "mytypes.h"
  55. #include "globals.h"
  56. #include "utils.h"
  57. #include "windowstuff.h"
  58. #include "myaocetypes.h"
  59. #include "myaoce.h"
  60.  
  61. #include "commands.h"
  62.  
  63. /* closes a window */
  64.  
  65. void CommCloseWindow(WindowPtr window)
  66. {
  67.     WInfoHndl infoHndl;
  68.     Boolean shouldClose;
  69.     
  70.     if (!IsAppWindow(window))
  71.         return;
  72.         
  73.     infoHndl = GetWindowInfo(window);
  74.     
  75.     if (!CanCloseLetter(window))
  76.         shouldClose = false;
  77.     else {
  78.         if ((**infoHndl).changed)
  79.             shouldClose = WarnOnWindowClose(window);
  80.         else
  81.             shouldClose = true;
  82.     }
  83.     
  84.     if (shouldClose) {
  85.         SendWindowMessage(window,kDeactivateMessage,nil);        
  86.         SendWindowMessage(window,kDestroyMessage,nil);
  87.         
  88.         DisposHandleChk(infoHndl);    
  89.         DisposeWindow(window);
  90.     }
  91.     else gDone = false;    // we might be quitting, so we'd want to abort that
  92. }
  93.  
  94.  
  95. /* processes edit menu commands */
  96.  
  97. void CommEdit(WindowPtr window,short command)
  98. {
  99.     short msg;
  100.     
  101.     if (!IsAppWindow(window))
  102.         return;
  103.  
  104.     switch (command) {
  105.         case kUndoItem:
  106.             msg = kUndoMessage;
  107.             break;
  108.         case kCutItem:
  109.             msg = kCutMessage;
  110.             break;
  111.         case kCopyItem:
  112.             msg = kCopyMessage;
  113.             break;
  114.         case kPasteItem:
  115.             msg = kPasteMessage;
  116.             break;
  117.         case kClearItem:
  118.             msg = kClearMessage;
  119.             break;
  120.         case kSelectAllItem:
  121.             msg = kSelectAllMessage;
  122.             break;
  123.         default:
  124.             return;
  125.     }
  126.     SendWindowMessage(window,msg,nil);
  127. }
  128.  
  129.  
  130. /* handles preference changing */
  131.  
  132. void CommEditPreferences(void)
  133. {
  134.     DialogPtr theDialog;
  135.     short iType;
  136.     Handle iHndl;
  137.     Rect iRect;
  138.     short item;
  139.     Boolean *prefSwitchPtr;
  140.     MyPreferences newPreferences;
  141.     
  142.     BlockMove(&gPreferences,&newPreferences,sizeof(MyPreferences));
  143.     
  144.     theDialog = GetNewDialog(kPrefsDialogID,nil,(WindowPtr)-1L);
  145.     MyDrawDefaultButtonOutline(theDialog,ok);
  146.     
  147.     GetDItem(theDialog,kCloseAfterSave,&iType,&iHndl,&iRect);
  148.     SetCtlValue((ControlHandle)iHndl,newPreferences.closeOnSend);
  149.     GetDItem(theDialog,kShowCloseOptions,&iType,&iHndl,&iRect);
  150.     SetCtlValue((ControlHandle)iHndl,newPreferences.closeOptionsDialog);
  151.     GetDItem(theDialog,kExpandMailerCreate,&iType,&iHndl,&iRect);
  152.     SetCtlValue((ControlHandle)iHndl,newPreferences.expandOnCreate);
  153.     GetDItem(theDialog,kExpandMailerOpen,&iType,&iHndl,&iRect);
  154.     SetCtlValue((ControlHandle)iHndl,newPreferences.expandOnOpen);
  155.     
  156.     do {
  157.         ModalDialog(nil,&item);
  158.         if (item>=kCloseAfterSave && item<=kExpandMailerOpen) {
  159.             switch (item) {
  160.                 case kCloseAfterSave:
  161.                     prefSwitchPtr = &newPreferences.closeOnSend;
  162.                     break;
  163.                 case kShowCloseOptions:
  164.                     prefSwitchPtr = &newPreferences.closeOptionsDialog;
  165.                     break;
  166.                 case kExpandMailerCreate:
  167.                     prefSwitchPtr = &newPreferences.expandOnCreate;
  168.                     break;
  169.                 case kExpandMailerOpen:
  170.                     prefSwitchPtr = &newPreferences.expandOnOpen;
  171.                     break;
  172.             }
  173.             *prefSwitchPtr = !(*prefSwitchPtr);
  174.             GetDItem(theDialog,item,&iType,&iHndl,&iRect);
  175.             SetCtlValue((ControlHandle)iHndl,*prefSwitchPtr);
  176.         }
  177.         
  178.     } while (item!=ok && item!=cancel);
  179.     
  180.     if (item==ok)
  181.         BlockMove(&newPreferences,&gPreferences,sizeof(MyPreferences));
  182.     
  183.     DisposeDialog(theDialog);
  184. }
  185.  
  186.  
  187. /* processes print commands */
  188.  
  189. void CommPrint(WindowPtr window)
  190. {
  191.     if (IsAppWindow(window))
  192.         SendWindowMessage(window,kPrintMessage,nil);
  193. }
  194.  
  195.  
  196. /* processes page setup commands */
  197.  
  198. void CommPageSetup(WindowPtr window)
  199. {
  200.     WInfoPtr infoPtr;
  201.     char hState;
  202.     Boolean changed;
  203.     
  204.     SetCursor(&qd.arrow);
  205.  
  206.     if (!IsAppWindow(window))
  207.         return;
  208.     
  209.     infoPtr = BeginWindowAccess(window,&hState);
  210.     
  211.     PrOpen();
  212.     changed = PrStlDialog(infoPtr->printRecord);
  213.     if (PrError()!=noErr)
  214.         DoError(PrError());
  215.     PrClose();
  216.     
  217.     if (changed)
  218.         SendWindowMessage(window,kPageSetupMessage,nil);
  219.  
  220.     EndWindowAccess(window,hState);
  221. }
  222.  
  223.  
  224. /* show about box */
  225.  
  226. void CommAbout(void)
  227. {
  228.     DialogPtr theDlg;
  229.     short item;
  230.     
  231.     theDlg = GetNewDialog(kAboutBoxID,nil,(WindowPtr)-1L);
  232.     ModalDialog(nil,&item);
  233.     DisposeDialog(theDlg);
  234. }
  235.  
  236.  
  237. /* new command */
  238.  
  239. void CommNew(void)
  240. {
  241.     MakeWindow(kDrawWindow,nil,nil,true);
  242. }
  243.  
  244.  
  245. /* open command */
  246.  
  247. void CommOpen(void)
  248. {
  249.     StandardFileReply sfReply;
  250.     SFTypeList typeList;
  251.     OSErr err;
  252.     WindowPtr window;
  253.     
  254.     typeList[0] = kDrawingType;
  255.     typeList[1] = kCDLtrMsgType;
  256.     
  257.     StandardGetFile(nil,2,typeList,&sfReply);
  258.     if (sfReply.sfGood) {
  259.         err = LoOpen(true, &sfReply.sfFile, nil,true,&window);
  260.         if (err!=noErr)
  261.             DoError(err);
  262.     }
  263. }
  264.  
  265.  
  266. /* low-level open (can be called by aevt) */
  267.  
  268. OSErr LoOpen(Boolean diskForm, FSSpec *fSpec, LetterSpec *lSpec,Boolean showWindow,WindowPtr *openedWindow)
  269. {
  270.     WindowPtr window;
  271.     WInfoHndl info;
  272.     short wType;
  273.     OSErr err;
  274.     FInfo fInfo;
  275.     LetterDescriptor letterDesc;
  276.     void *message;
  277.     RString name;
  278.     
  279.     name.charSet = smRoman;
  280.     name.dataLength = 0;
  281.     
  282.     *openedWindow = nil;
  283.  
  284.     if (diskForm) {
  285.         err = FSpGetFInfo(fSpec,&fInfo);
  286.         if (err!=noErr)
  287.             return err;
  288.         
  289.         switch (fInfo.fdType) {
  290.             case kDrawingType:
  291.                 wType = kDrawWindow;
  292.                 message = fSpec;
  293.                 break;
  294.             case kCDLtrMsgType:
  295.                 wType = kDrawMailerWindow;
  296.                 letterDesc.onDisk = true;
  297.                 BlockMove(fSpec,&letterDesc.u.fileSpec,sizeof(FSSpec));
  298.                 message = &letterDesc;
  299.                 break;
  300.             default:
  301.                 return kInternalError;
  302.         }
  303.     }
  304.     else {
  305.         wType = kDrawMailerWindow;
  306.         letterDesc.onDisk = false;
  307.         BlockMove(lSpec,&letterDesc.u.mailboxSpec,sizeof(LetterSpec));
  308.         message = &letterDesc;
  309.     }
  310.         
  311.         
  312.     window = MakeWindow(wType,nil,nil,false);
  313.  
  314.     if (window) {
  315.         info = GetWindowInfo(window);
  316.         SendWindowMessage(window,kLoadMessage,message);
  317.         if (diskForm)
  318.             BlockMove(fSpec->name, name.body - 1, fSpec->name[0] + 1);
  319.         else {
  320.             (**info).saved = false;
  321.             err = SMPGetComponentInfo(window, 1, kSMPRegarding, &name);
  322.             BlockMove(name.body - 1,fSpec->name,name.dataLength+1);
  323.             if (err!=noErr)
  324.                 return err;
  325.         }
  326.         SetWTitle(window, name.body - 1);
  327.         BlockMove(fSpec,&(*info)->fileSpec,sizeof(FSSpec));
  328.         if (showWindow)
  329.             ShowWindow(window);
  330.     }
  331.     *openedWindow = window;
  332.     return noErr;
  333. }
  334.  
  335.  
  336. /* save command- returns false if user cancelled save */
  337.  
  338. Boolean CommSaveFile(WindowPtr window)
  339. {
  340.     WInfoHndl info;
  341.     
  342.     if (!IsAppWindow(window))
  343.         return true;
  344.     
  345.     info = GetWindowInfo(window);
  346.     if (!(**info).changed)
  347.         return true;
  348.         
  349.     if (!(**info).saved)
  350.         return CommSaveAsFile(window);
  351.     else {
  352.         LoSaveFile(window,kSMPSave);
  353.         return true;
  354.     }
  355. }
  356.  
  357.  
  358. /* save as command */
  359.  
  360. Boolean CommSaveAsFile(WindowPtr window)
  361. {
  362.     WInfoHndl info;
  363.     StandardFileReply sfReply;
  364.     Str255 promptString;
  365.     Str255 defaultFilename;
  366.     StringPtr fileName;
  367.     WindowKind kind;
  368.     
  369.     info = GetWindowInfo(window);
  370.  
  371.     fileName = (**info).fileSpec.name;
  372.     if (fileName[0]==0) {
  373.         GetResString(defaultFilename,kDefaultFilenameID,kDefaultFilename);
  374.         fileName = defaultFilename;
  375.     }
  376.         
  377.     if (!IsAppWindow(window))
  378.         return true;
  379.     
  380.     kind = GetWindowKind(window);
  381.     switch (kind) {
  382.         case kDrawWindow:
  383.             GetResString(promptString,kPromptStringID,kPromptString);
  384.             break;
  385.         case kDrawMailerWindow:
  386.             GetResString(promptString,kSaveLetterAsID,kSaveLetterAs);
  387.             break;
  388.     }
  389.  
  390.     StandardPutFile(promptString,fileName,&sfReply);
  391.     if (sfReply.sfGood) {
  392.         BlockMove(&sfReply.sfFile,&((**info).fileSpec),sizeof(FSSpec));
  393.         LoSaveFile(window,kSMPSaveAs);
  394.         return true;
  395.     }
  396.     else
  397.         return false;
  398. }
  399.  
  400.  
  401. /* low-level save file */
  402.  
  403. void LoSaveFile(WindowPtr window,SMPSaveType how)
  404. {
  405.     SendWindowMessage(window,kSaveMessage,(void *)how);
  406. }
  407.  
  408.  
  409. Boolean WarnOnWindowClose(WindowPtr window)
  410. {
  411.     short item;
  412.     WInfoPtr infoPtr;
  413.     char hState;
  414.     Boolean returnValue;
  415.     WindowKind kind;
  416.     Str63 documentKind,quitReason;
  417.     
  418.     kind = GetWindowKind(window);
  419.     switch (kind) {
  420.         case kDrawWindow:
  421.             GetResString(documentKind,kDrawingID,kDrawing);
  422.             break;
  423.         case kDrawMailerWindow:
  424.             GetResString(documentKind,kLetterID,kLetter);
  425.             break;
  426.     }
  427.     if (gDone)
  428.         GetResString(quitReason,kQuittingID,kQuitting);
  429.     else
  430.         GetResString(quitReason,kClosingID,kClosing);
  431.         
  432.     infoPtr = BeginWindowAccess(window,&hState);
  433.     ParamText(documentKind,infoPtr->fileSpec.name,quitReason,nil);
  434.     EndWindowAccess(window,hState);
  435.     
  436.     SetCursor(&qd.arrow);
  437.     item = StopAlert(kWarnCloseID,nil);
  438.     
  439.     switch (item) {
  440.         case cancel:
  441.             returnValue = false;
  442.             break;
  443.         case kDontSave:
  444.             returnValue = true;
  445.             break;
  446.         case ok:
  447.             returnValue = CommSaveFile(window);
  448.             break;
  449.     }
  450.     return returnValue;
  451. }
  452.